package com.xugongzhang.springcloud.alibaba.service.impl;

import com.alibaba.fastjson.JSON;
import com.xugongzhang.springcloud.alibaba.dao.OrderDao;
import com.xugongzhang.springcloud.alibaba.domain.Order;
import com.xugongzhang.springcloud.alibaba.service.AccountService;
import com.xugongzhang.springcloud.alibaba.service.OrderService;
import com.xugongzhang.springcloud.alibaba.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author X
 * @date 2022/6/19 2:44
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Resource
    private StorageService storageService;

    @Resource
    private AccountService accountService;

    @Override
    public void create(Order order) {

        log.info("------》新建定单{}", JSON.toJSONString(order));
        orderDao.create(order);

        log.info("------》定单微服务调用库存微服务--减库存count");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("------》定单微服务调用账户微服务,减账户余额money");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("------》扣减结束");

        // 修改定单状态 从 0 到 1 已完成
        log.info("------》修改定单状态start");
        orderDao.update(order.getUserId(), 0);
        log.info("------》修改定单状态end");
        log.info("------》下订单流程结束");
    }
}
